#include<sys/types.h>
#include<unistd.h>
#include<queue>
#include<semaphore.h>
#include<iostream>
using namespace std;

const int gCap=5;
template<class T>
class RingQueue
{
public:
    RingQueue(int cap=gCap):ringqueue_(cap),pIndex_(0),cIndex_(0)
    {
        sem_init(&roomSem_,0,ringqueue_.size());
        sem_init(&dataSem_,0,0);
    }
    void push(const T& in)
    {
        sem_wait(&roomSem_);
        ringqueue_[pIndex_]=in;
        sem_post(&dataSem_);
        pIndex_++;
        pIndex_%=ringqueue_.size();
    }
    T pop()
    {
        sem_wait(&dataSem_);
        T temp=ringqueue_[cIndex_];
        sem_post(&roomSem_);
        cIndex_++;
        cIndex_%=ringqueue_.size();
        return temp;
    }
    ~RingQueue()
    {
        sem_destroy(&roomSem_);
        sem_destroy(&dataSem_);
    }
private:
    vector<T> ringqueue_;
    sem_t roomSem_; 
    sem_t dataSem_;
    uint32_t cIndex_;
    uint32_t pIndex_;
};